package com.gw.redis.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class RedisSetUtil {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean sAdd(final String key,Object ... value){
        boolean result=false;
        try {
            redisTemplate.opsForSet().add(key,value);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 读取缓存
     * @param key
     * @return
     */
    public Set sMembers(final String key){
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 删除缓存
     * @param key
     * @param value
     */
    public long sRemove(final String key,Object ... value){
        return redisTemplate.opsForSet().remove(key,value);
    }

    /**
     * 更新缓存
     * @param key
     * @param value1
     * @param value2
     * @return
     */
    public boolean sGetAndSet(final String key,Object value1,Object value2){
        boolean result=false;
        try {
            redisTemplate.opsForSet().remove(key,value1);
            redisTemplate.opsForSet().add(key,value2);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
